import java.io.IOException;

public class Receiver extends Thread {
	StreamSocket client;
	String clientInput;
	String buffer;

	public Receiver(StreamSocket client) {
		this.client = client;
	}

	private void addToBuffer(char clientInputChar) {
		buffer += clientInputChar;
		if (buffer.equals("\n")) {
			buffer = "";
		}
		buffer = buffer.replaceAll("null", "");
		if (buffer.contains("\r\n\r\n")) {
			setInput(buffer);
			buffer = "";
		}
	}

	private void setInput(String clientInput) {
		this.clientInput = clientInput;
	}

	public synchronized String getInput() {
		return clientInput;
	}

	public synchronized void clear() {
		clientInput = "";
	}

	public void run() {
		while (client.isOpen()) {
			try {
				addToBuffer(client.readChar());
			} catch (IOException e) {
				try {
					client.kill();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
}
